﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevTeam;

namespace ConfExpert.Forms.ImportWizards.Siparis
{
    public partial class XIHolyFashionNumune : XImportWizard
    {
        private string FileName { get; set; }
        public XIHolyFashionNumune()
        {
            InitializeComponent();
            this.ProcessStart += XIHolyFashion_ProcessStart;
            this.ProcessDone += XIHolyFashion_ProcessDone;
            this.FileDone += XIHolyFashion_FileDone;
            btnSave.ItemClick += btnSave_ItemClick;
        }
                
        void btnSave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (Order.Rows.Count == 0)
            {
                ShowErrorMessage("Aktarılacak sipariş yok !");
                return;
            }

            if (gleSezon.EditValue.ToInt()==0)
            {
                ShowErrorMessage("SezonId boş olamaz !");
                return;
            }

            if (gleTalepTipi.EditValue.ToInt() == 0)
            {
                ShowErrorMessage("Talep tipi boş olamaz !");
                return;
            }

            if (txtOrderRef.EditValue.ToStr() == "")
            {
                ShowErrorMessage("OrderRef boş olamaz !");
                return;
            }

            if (bbiSezon.EditValue.ToStr() == "")
            {
                ShowErrorMessage("Sezon boş olamaz !");
                return;
            }

            string xml = Order.Serialize();
            ExecuteNonQuery("InsHFOrderNumune", xml, gleSezon.EditValue, gleTalepTipi.EditValue, txtOrderRef.EditValue, FileName,bbiSezon.EditValue);
            
            Order.Clear();
            gleSezon.EditValue = gleTalepTipi.EditValue = null;
            txtOrderRef.EditValue = "";
            btnSave.Enabled = false;
            ShowInfoMessage("Siparişiniz aktarıldı...");
        }

        void XIHolyFashion_ProcessDone()
        {
            gvMaster.Init(Order);
        }

        void XIHolyFashion_ProcessStart()
        {
            Order = new DataTable();

            Order.Columns.Add("Brand", typeof(string));
            Order.Columns.Add("OrderNr", typeof(string));
            Order.Columns.Add("PO", typeof(string));
            Order.Columns.Add("Model", typeof(string));
            Order.Columns.Add("Artikel", typeof(string));
            Order.Columns.Add("Renk", typeof(string));
            Order.Columns.Add("Beden", typeof(string));
            Order.Columns.Add("Adet", typeof(int));

        }
        public DataTable Order;
        private void XIHolyFashion_Load(object sender, EventArgs e)
        {
            splitter.PanelVisibility = DevExpress.XtraEditors.SplitPanelVisibility.Panel1;
            gleSezon.Edit.Assign(RI(Rp.Sezon));
            gleTalepTipi.Edit.Assign(RI(Rp.TalepTipi));
        }
        void XIHolyFashion_FileDone(DataSet Data, string FileName)
        {
            DataTable dt = Data.Tables[0];

            Point pt = FindPositionByValue(dt, "Brand");//"KARBEL" isimli çoğu zaman
            DataRowCollection rows = dt.Rows;
            DataColumnCollection cols = dt.Columns;
            Dictionary<string, int> indexes = new Dictionary<string, int>();

            DataRow dr0 = dt.Rows[pt.X];
            for (int colIndex = 0; colIndex < dt.Columns.Count; colIndex++)
            {
                switch (dr0[colIndex].ToString().ToLower())
                {
                    case "brand": indexes.Add("Brand", colIndex); break;
                    case "po":
                    case "order no.": indexes.Add("OrderNr", colIndex); break;
                    case "product number":
                    case "product no.": indexes.Add("PO", colIndex); break;
                    case "style": 
                    case "product name": indexes.Add("Model", colIndex); break;
                    case "article": indexes.Add("Artikel", colIndex); break;
                    case "colour": indexes.Add("Renk", colIndex); break;
                    case "size": indexes.Add("Beden", colIndex); break;
                    case "qty": indexes.Add("Adet", colIndex); break;
                }
            }

            DataTable dtdagilim = ExecuteDataTableByQuery("select * from dagilimVertical where dagilimId in (10,36) and Beden <> ''");
            
            for (int i = pt.X+1; i < dt.Rows.Count; i++)
            {
                DataRow dr = rows[i];
                if (dr[pt.Y].ToStr() == "")
                    continue;

                DataRow drOrder = Order.NewRow();
                foreach (KeyValuePair<string, int> k in indexes)                    
                        drOrder[k.Key] = dr[k.Value];

                Order.Rows.Add(drOrder);
                this.FileName = FileName;
            }
        }

        public Point FindPositionByValue(DataTable dt, string value)
        {
            Point loc = new Point();

            bool OuterBreak = false;
            for (int dc = 0; dc < dt.Columns.Count; dc++)
            {
                string ColumnName = dt.Columns[dc].ColumnName;
                for (int dr = 0; dr < dt.Rows.Count; dr++)
                {
                    string CellValue = dt.Rows[dr][ColumnName].ToStr();
                    if (CellValue.ToLower() == value.ToLower())
                    {
                        loc.X = dr;
                        loc.Y = dc;
                        OuterBreak = true;
                        break;
                    }
                }
                if (OuterBreak) break;
            }
            return loc;
        }
    }
}
